package br.com.massai.view.converters;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

@FacesConverter(value = "accrualBasisConverter")
public class AccrualBasisConverter implements Converter {

	@Override
	public Object getAsObject(FacesContext context, UIComponent component, String value) {
		if(value == null || value.equals("") || value.equals("__/____"))
			return null;
		else{
			String accrualBasis = value.replace("/", "");
			return accrualBasis;
		}
	}

	@Override
	public String getAsString(FacesContext context, UIComponent component, Object value) {
		if(value != null){
			StringBuilder accrualBasis = new StringBuilder();
			String accrualBasisStr = value.toString();
			if(accrualBasisStr.length() == 6)
				accrualBasis.append(accrualBasisStr.substring(0, 2)).append("/").append(accrualBasisStr.substring(2, 6));
			return accrualBasis.toString();
		}
		return null;
	}
}